﻿#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

const int N = 2e4 + 10;
//存储左右区间
int a[N], b[N];
//离散化数组
int disc[2 * N];//小-》大
//差分数组
int f[2 * N];

int pos;
unordered_map<int, int> id;//大-》小

int n;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i] >> b[i];
		//存储在离散化数组中
		disc[++pos] = a[i]; disc[++pos] = b[i];
	}
	//进行离散化
	sort(disc + 1, disc + 1 + pos);
	//去重
	pos = unique(disc + 1, disc + 1 + pos) - (disc + 1);
	//利用hash表存储下标，方便查询
	for (int i = 1; i <= pos; i++) id[disc[i]] = i;
	//差分
	for (int i = 1; i <= n; i++)
	{
		int l = id[a[i]], r = id[b[i]];
		f[l]++; f[r]--;
	}
	//还原差分数组
	for (int i = 1; i <= pos; i++) f[i] += f[i - 1];
	//求解
	int ret = 0;
	for (int i = 1; i <= pos; i++)
	{
		int j = i;
		while (j <= pos && f[j] > 0) j++;
		ret += disc[j] - disc[i];
		i = j;
	}
	cout << ret << endl;
	return 0;
}